package net.huanger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSupport;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * Generator by JdbcGenerator
 */
@Repository
public class HelloDao extends NamedParameterJdbcDaoSupport {
    private static String TABLE_NAME="";
    public static final String COUNT_ALL = "";
    public static final String INSERT_ALL = "";
    private static final String UPDATE_ALL = "";
    public static RowMapper<Hello> mainRowMapper =new  RowMapper<Hello>(){
        @Override
        public Hello mapRow(ResultSet resultSet, int i) throws SQLException {
            Hello bean=new Hello();

            return bean;
        }
    };
    @Autowired
    public HelloDao(DataSource dataSource){
        setDataSource(dataSource);
    }
    public Hello save(Hello bean){
        String insertSql= INSERT_ALL;
        SqlParameterSource params=new BeanPropertySqlParameterSource(bean);
        GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();
        getNamedParameterJdbcTemplate().update(insertSql,params, generatedKeyHolder);
        int pk = generatedKeyHolder.getKey().intValue();
        bean.setId(pk);
        return bean;
    }
    public Long count() {
        Long total=getJdbcTemplate().queryForObject(COUNT_ALL,Long.class);
        return total;
    }
    public Hello findByPrimaryKey(Integer id) {
        Hello bean = getJdbcTemplate().queryForObject("select * from alumnus where ID=?", mainRowMapper, id);
        return bean;
    }
    public void deleteByPrimaryKey(Integer id) {
        getJdbcTemplate().update("delete from alumnus where ID=?",id);
    }
    public void update(Hello bean) {
        String insertSql= UPDATE_ALL;
        SqlParameterSource params=new BeanPropertySqlParameterSource(bean);
        getNamedParameterJdbcTemplate().update(insertSql, params);
    }

    public List<Hello> findAll() {
        List<Hello> query = getJdbcTemplate().query("select * from association_structure", mainRowMapper);
        return query;
    }
}
